<?php
namespace app\admin\controller;
class PkCat extends Common
{
    public function lst()
    {
        $level = input()['level'];
        $cateRes=db('pk_group')->where('level',$level)->order('id')->select();
        foreach ($cateRes as $k=>$v){
            if($v['admins']){
                $admin_Arr = explode(',', $v['admins']);
                $cateRes[$k]['admin_names'] = '';
                foreach ($admin_Arr as $k1=>$v1){
                    if($k1!==0){
                        $cateRes[$k]['admin_names'] .='&nbsp;&nbsp;'.db('admin')->where('id',$v1)->value('name');
                    }else{
                        $cateRes[$k]['admin_names'] .=db('admin')->where('id',$v1)->value('name');
                    }
                }
            }else{
                $cateRes[$k]['admin_names']='';
            }
        }
        $this->assign('cateRes',$cateRes);
        $count=db('pk_group')->count();
        $this->assign('count',$count);
        return view();
    }

    public function add()
    {
        $level = input()['level'];
        if(request()->isAjax()){
            $data=input('post.');
            if(isset($data['admins'])){
                if($data['level']==2)
                {
                    foreach ($data['admins'] as $k => $v) 
                    {
                        $group_members = db('admin')->where('id',$v)->value('group_members');
                        if($group_members)
                        {
                            $data['admins'][] = $group_members;
                        }
                    }
                }
                
                $admins=implode(',', $data['admins']);
                $data['admins']=$admins;
            }

            $add=db('pk_group')->insert($data);
            if($add){
                return ['msg'=>'新增PK组成功','icon'=>1];
            }else{
                return ['msg'=>'新增PK组失败','icon'=>2];
            }
            return;
        }
        //所有员工
        //$adminRes=db('admin')->where('group_id','<>',1)->where('group_id','<>',6)->order('id')->select();

        if($level == 1 || $level == 2)
        {
            $adminRes=db('admin')->where('group_id',5)->where('status',1)->order('id')->select();
            foreach ($adminRes as $k => $v) 
            {
                if($v['group_members'])
                {
                    $adminRes[$k]['children'] = db('admin')->where('status',1)->where('id','IN',$v['group_members'])->order('id')->select();
                    $group_members = $group_members?$group_members.','.$v['group_members']:$v['group_members'];
                }
                
            }
            if($group_members)
            {
                $adminRes1=db('admin')->where('group_id',2)->where('status',1)->where('id','NOT IN',$group_members)->order('id')->select();
            }
        }
        else 
        {
            $adminRes=db('admin')->where('group_id','IN','2,5')->where('status',1)->order('id')->select();
            $admin_Arr = [];
            $_admin_Arr=db('pk_group')->where('level',3)->field('admins')->select();
            foreach ($_admin_Arr as $k=>$v)
            {
                if($v['admins']){
                    $arr = explode(',', $v['admins']);
                    foreach ($arr as $v){
                        $admin_Arr[] = $v;
                    }
                }
            }
        }

        
        //判断是否已经在其他组中
        /*$admin_Arr = [];
        $_admin_Arr=db('pk_group')->field('admins')->select();
        foreach ($_admin_Arr as $k=>$v){
            if($v['admins']){
                $arr = explode(',', $v['admins']);
                foreach ($arr as $v){
                    $admin_Arr[] = $v;
                }
            }
        }*/
        $this->assign([
            'adminRes'=>$adminRes,
            'adminRes1'=>$adminRes1,
            'admin_Arr'=>$admin_Arr
        ]);
        return view();
    }

    public function edit()
    {
        if(request()->isAjax()){
            $data=input('post.');
            if(isset($data['admins'])){
                $admins=implode(',', $data['admins']);
                $data['admins']=$admins;
            }else{
                $data['admins']='';
            }
            $save=db('pk_group')->update($data);
            if($save !== false){
                return ['msg'=>'编辑PK组成功','icon'=>1];
            }else{
                return ['msg'=>'编辑PK组失败','icon'=>2];
            }
            return;
        }
        $id = input('id');
        $pk_cat = db('pk_group')->find($id);
        $level = $pk_cat['level'];
        //当前组内员工
        $admin_arr=explode(',', $pk_cat['admins']);

        if($level == 1 || $level == 2)
        {
            $adminRes=db('admin')->where('group_id',5)->where('status',1)->order('id')->select();
            foreach ($adminRes as $k => $v) 
            {
                if($v['group_members'])
                {
                    $adminRes[$k]['children'] = db('admin')->where('status',1)->where('id','IN',$v['group_members'])->order('id')->select();
                    $group_members = $group_members?$group_members.','.$v['group_members']:$v['group_members'];
                }
                
            }
            if($group_members)
            {
                $adminRes1=db('admin')->where('group_id',2)->where('status',1)->where('id','NOT IN',$group_members)->order('id')->select();
            }
        }
        else 
        {
            $adminRes=db('admin')->where('group_id','IN','2,5')->where('status',1)->order('id')->select();
            $admin_Arr = [];
            $_admin_Arr=db('pk_group')->where('level',3)->where('id','<>',$id)->field('admins')->select();
            foreach ($_admin_Arr as $k=>$v)
            {
                if($v['admins']){
                    $arr = explode(',', $v['admins']);
                    foreach ($arr as $v){
                        $admin_Arr[] = $v;
                    }
                }
            }
        }

        
        //所有员工
        /*$adminRes=db('admin')->where('group_id','<>',1)->order('id')->select();
        
        //判断是否已经在其他组中
        $admin_Arr = [];
        $_admin_Arr=db('pk_group')->field('admins')->where('id','<>',$id)->select();
        foreach ($_admin_Arr as $k=>$v){
            if($v['admins']){
                $arr = explode(',', $v['admins']);
                foreach ($arr as $v){
                    $admin_Arr[] = $v;
                }
            }
        }*/
        $this->assign([
            'level'=>$level,
            'pk_cat'=>$pk_cat,
            'admin_arr'=>$admin_arr,
            'adminRes'=>$adminRes,
            'adminRes1'=>$adminRes1,
            'admin_Arr'=>$admin_Arr
        ]);
        return view();
    }

    public function del()
    {
        if(request()->isAjax()){
            $id=input('id');

            $del=db('pk_group')->delete($id);
            if($del){
                return ['msg'=>'删除Pk组成功','icon'=>1];
            }else{
                return ['msg'=>'删除Pk组失败','icon'=>2];
            }
        } 
    }


}
